<template>
  <div class="login">
    <!-- 头部返回栏 -->
    <van-nav-bar>
      <template #left>
        <i class="iconfont iconbtn_nav_back" @click="routeBack"></i>
      </template>
    </van-nav-bar>
    <!-- 标题 -->
    <h5>您好, 请登录</h5>
    <!-- 内容 -->
    <div class="major">
      <van-form @submit="onSubmit">
        <!--1 输入框部分 -->
        <van-field
          v-model="user.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        >
          <!-- 左边插槽 -->
          <template #left-icon>
            <i class="iconfont iconbianzu1"></i>
          </template>
        </van-field>
        <van-cell-group>
          <!-- 2 验证码部分 -->
          <van-field
            v-model="user.code"
            :rules="rules.code"
            placeholder="请填写验证码"
          >
            <!-- 左边插槽 -->
            <template #left-icon>
              <i class="iconfont iconyanzhengma"></i>
            </template>
            <!-- 右边插槽 -->
            <template #button>
              <van-button
                class="codebtn"
                type="mini"
                plain
                @click.stop.prevent="getCode"
                :disabled="time !== 30"
              >
                <span v-if="time === 30">获取验证码</span>
                <span v-else>{{ time }}s后重试</span>
              </van-button>
            </template>
          </van-field>
        </van-cell-group>
        <!--协议说明部分 -->
        <div class="state">
          登录即同意 <span>《用户使用协议》</span>和<span>《隐私协议》</span>
        </div>
        <!-- 大按钮 -->
        <div class="bigBtn">
          <van-button
            type="danger"
            size="large"
            round
            :loading="isLoad"
            loading-text="加载中..."
            >确定</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { getCode, login } from '@/api/api.js'
import { setLocal } from '@/utils/local.js'
// import { mapMutations } from 'vuex'
export default {
  // ...mapMutations(['SETUSERINFO']),
  data () {
    return {
      // 导入基地址
      baseURL: process.env.VUE_APP_URL,
      // 用户信息
      user: {
        mobile: '18888881111',
        code: ''
      },
      // 验证规则
      rules: {
        mobile: [
          //  验证是否为空
          { required: true, message: '请输入手机号', trigger: 'onBlur' },
          // 验证是否匹配正则
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          // 验证是否为空
          { required: true, message: '请输入验证码', trigger: 'onBlur' },
          // 验证是否匹配正则
          { pattern: /^\d{4}$/, message: '验证码长度为4', trigger: 'onBlur' }
        ]
      },
      // myuser: {},
      // 倒计时时间
      time: 30,
      // 是否处于加载状态
      isLoad: false,
      userinfo: {}
    }
  },
  methods: {
    // 返回上一个页面
    routeBack () {
      this.$router.back()
    },
    // 获取验证码方法
    async getCode () {
      // 点击验证码按钮弹出框一直转动
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      // 发送获取验证码请求
      const res = await getCode({ mobile: this.user.mobile })
      // 弹出响应成功的验证码
      this.$toast.success(`${res.data}`)
      // 把验证码传到输入框
      this.$nextTick(() => {
        this.user.code = res.data
        this.tiemCode()
      })
    },
    // 登陆方法
    async onSubmit () {
      // 判断是否正在发请求
      if (this.isLoad === true) {
        return
      }
      try {
        // 提交请求前加载数据改为true
        this.isLoad = true
        // 发送登陆请求
        const res = await login(this.user)
        // 发送请求后加载数据改为false
        this.isLoad = false
        // token保存到本地
        setLocal(res.data.jwt)
        // // 保存获取的用户数据 到vuex
        res.data.user.avatar = this.baseURL + res.data.user.avatar
        this.$store.commit('SETUSERINFO', res.data.user)
        this.userinfo = res.data.user
        // 登录提醒
        this.$toast.success(res.message)
        if (this.$route.path === '/checklogin') {
          this.$router.go(-1)
        } else {
          // 跳转到我的页面
          this.$router.push('/my')
        }
      } catch (error) {
        this.isLoad = false
        this.$toast('手机号或验证码有误' + error.message)
      }
    },
    // 计算器方法
    tiemCode () {
      this.time--
      const tiemID = setInterval(() => {
        this.time--
        if (this.time === 0) {
          clearInterval(tiemID)
          this.time = 30
        }
      }, 1000)
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  .iconbtn_nav_back{
    font-size: 40px;
  }
  /deep/ .van-nav-bar__left {
    padding: 0 3px;
  }
  /deep/.icon-zuojiantou {
    font-size: 30px;
    padding: 0px;
  }
  h5 {
    padding-left: 15px;
    margin-top: 47px;
    margin-bottom: 60px;
  }

  .major {
    padding: 0 15px;
    .van-button--large {
      width: 100%;
      height: 41px;
    }
    .state {
      font-size: 12px;
      margin: 15px 0;
      margin-bottom: 44px;
      span {
        color: #00b8d4;
      }
    }
    /deep/ .van-field__button .van-button--mini {
      height: 24px;
      padding: 0 4px;
      font-size: 16px;
      border: 0;
      color: #00b8d4;
    }
  }
}
</style>
